//
//  UIBaseMainViewController.m
//  LollipopApp
//
//  Created by zhangyongbing on 15/12/11.
//  Copyright © 2015年 zhangyongbing. All rights reserved.
//

#import "UIBaseMainViewController.h"
#import "MYLoginViewController.h"
#import "MYReplenishmentViewController.h"
#import "MYPersonalCenterViewController.h"
#import "MYReplenishmentListViewController.h"
#import "MYVendingMachineListViewController.h"
#import "MYPersonal_HttpAPI.h"

@interface UIBaseMainViewController ()
{
    BOOL isInlogin;
}

@end

@implementation UIBaseMainViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NEED_LOGIN object:nil];
}

- (void)loadView
{
    [super loadView];
//    [self openLoginView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    isInlogin = NO;
    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openLoginView) name:NEED_LOGIN object:nil];
    
    [self setChlidControllers];
}

- (void)viewDidAppear:(BOOL)animated
{
//    NSString * token = [[NSUserDefaults standardUserDefaults] valueForKey:@"access_token"];
//    if ([token stringIsValid]) {
//        [self getUserInfoFromServices];
//    }
}

- (void)getUserInfoFromServices
{
    [MYPersonal_HttpAPI myGetPersonalInformationWithcomplationHandler:^(OCJBaseResponceModel *responseModel) {
        NSDictionary *dic = responseModel.ocjDic_data;
        [[PublicDefine shareInstance].dicUserInfo removeAllObjects];
        [[PublicDefine shareInstance].dicUserInfo addEntriesFromDictionary:dic];
    }];

}

/**
 *  处理通知跳转事件
 *
 */
-(void)fx_receiveNotice:(NSNotification*)notice{
    
    
}

- (void)showLoginInAnother
{
    UIAlertView *alertDialog = [[UIAlertView alloc] initWithTitle:nil message:@"您的账号在另一台设备登录." delegate: self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    alertDialog.tag = 10001;
    [alertDialog show];
}

- (void)openLoginView
{
    MYLoginViewController *ctrl = [[MYLoginViewController alloc] initWithNibName:@"MYLoginViewController" bundle:nil];
    UINavigationController *LoginNavCtrl = [[UINavigationController alloc] initWithRootViewController: ctrl];
    LoginNavCtrl.navigationBarHidden = YES;
    [self presentViewController:LoginNavCtrl animated:YES completion:nil];
}

- (void)tokenTimeOut
{
    UIAlertView *alertDialog = [[UIAlertView alloc] initWithTitle:nil message:@"token错误，请重新登录" delegate: self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    alertDialog.tag = 10001;
    [alertDialog show];

}

- (void)setChlidControllers
{
    MYVendingMachineListViewController *home=[[MYVendingMachineListViewController alloc]initWithNibName:@"MYVendingMachineListViewController" bundle:nil];
    UINavigationController *homeNavCtrl = [[UINavigationController alloc] initWithRootViewController: home];
    homeNavCtrl.navigationBarHidden = YES;
    homeNavCtrl.tabBarItem.title=@"售货机";
    homeNavCtrl.tabBarItem.image=[[UIImage imageNamed:@"icon_Vending_machine"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    homeNavCtrl.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    MYReplenishmentListViewController *classify = [[MYReplenishmentListViewController alloc] initWithNibName:@"MYReplenishmentListViewController" bundle:nil];
    UINavigationController *classifyNavCtrl = [[UINavigationController alloc] initWithRootViewController: classify];
    classifyNavCtrl.navigationBarHidden = YES;
    classifyNavCtrl.tabBarItem.title=@"补货清单";
    classifyNavCtrl.tabBarItem.image=[UIImage imageNamed:@"icon_replenishment_list"];
//    classifyNavCtrl.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_fbrw"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    
    MYReplenishmentViewController *shopping=[[MYReplenishmentViewController alloc]initWithNibName:@"MYReplenishmentViewController" bundle:nil];
    UINavigationController *shopoingNavCtrl = [[UINavigationController alloc] initWithRootViewController: shopping];
    shopoingNavCtrl.navigationBarHidden = YES;
    shopoingNavCtrl.tabBarItem.title=@"补货";
    shopoingNavCtrl.tabBarItem.image=[UIImage imageNamed:@"icon_replenishment"];
//    shopoingNavCtrl.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_zr"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    MYPersonalCenterViewController *mine=[[MYPersonalCenterViewController alloc]initWithNibName:@"MYPersonalCenterViewController" bundle:nil];
    UINavigationController *mineNavCtrl = [[UINavigationController alloc] initWithRootViewController: mine];
    mineNavCtrl.navigationBarHidden = YES;
    mineNavCtrl.tabBarItem.title=@"我的";
    mineNavCtrl.tabBarItem.image=[UIImage imageNamed:@"icon_personal"];
//    mineNavCtrl.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_grxz"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.viewControllers = @[homeNavCtrl,classifyNavCtrl,shopoingNavCtrl,mineNavCtrl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 10001) {
        if (buttonIndex == 0) {
            [self openLoginView];
        }
    }
    
}

@end
